---
title: "Cleaning DALP dataset"
---

# Load

```{r}
# load packages
  source("helper-packages.R")

# load dalp party-level dataset
  dalp_raw <- 
    import("../raw-data/x-dalp/partylevel_20130907.dta")
```

Relevant questions:

****

a8_3: a8 recoded: Do the following parties have strong
linkages to Religious Organizations?
[0] No
[1] Yes

****

b10_3: b10 recoded: Do the parties most commonly rely
on Religious Organizations in their efforts to
select the recipients and deliver the benefits to
the target constituencies?
[0] No
[1] Yes

****

c2_3: If parties want to find out an
individual’s vote or a group’s vote, they may try to
use the knowledge of local organizations and their
leaders. Do the parties rely on Religious
organizations to provide information about an
individual’s or a group’s voting behavior?
[0] No
[1] Yes

****

partysize
Average of a party’s vote shares in the most recent two national legislative elections
before 2009.

# Clean 

```{r}
# clean
  dalp_clean <- 
    dalp_raw %>% 
    select(
      country,
      pengname,
      partysize,
      a8_3,
      b10_3,
      c2_3
      ) %>% 
    mutate(
      country_temp = 
        case_when(
          country == "S. Africa" ~ "South Africa",
          country == "Dom. Rep." ~ "Dominican Republic",
          country == "ROK" ~ "South Korea",
          TRUE ~ country),
      dalp_country_common = countryname(country_temp),
      party_size_prop = partysize/100) %>% 
    group_by(country, dalp_country_common) %>% 
      summarise(
        dalp_a8_3_party_links_to_rel_orgs_weighted = weighted.mean(a8_3, party_size_prop, na.rm = T),
        dalp_b10_3_relies_on_rel_orgs_to_target_benefits_weighted = weighted.mean(b10_3, party_size_prop, na.rm = T),
        dalp_c2_3_relies_on_rel_orgs_for_vote_info_weighted = weighted.mean(c2_3, party_size_prop, na.rm = T)) %>% 
    ungroup() %>% 
    select(starts_with("dalp_")) %>% 
    mutate(
      # compute medians of the three variables
      dalp_a8_3_party_links_to_rel_orgs_weighted_median = median(dalp_a8_3_party_links_to_rel_orgs_weighted, na.rm = T),
      dalp_b10_3_relies_on_rel_orgs_to_target_benefits_weighted_median = median(dalp_b10_3_relies_on_rel_orgs_to_target_benefits_weighted, na.rm = T),
      dalp_c2_3_relies_on_rel_orgs_for_vote_info_weighted_median = median(dalp_c2_3_relies_on_rel_orgs_for_vote_info_weighted, na.rm = T),
      
      # compute greater than median versions of the three variables
      dalp_a8_3_party_links_to_rel_orgs_weighted_above_median = (dalp_a8_3_party_links_to_rel_orgs_weighted >= dalp_a8_3_party_links_to_rel_orgs_weighted_median)*1,
      dalp_b10_3_relies_on_rel_orgs_to_target_benefits_weighted_above_median = (dalp_b10_3_relies_on_rel_orgs_to_target_benefits_weighted >= dalp_b10_3_relies_on_rel_orgs_to_target_benefits_weighted_median)*1,
      dalp_c2_3_relies_on_rel_orgs_for_vote_info_weighted_above_median = (dalp_c2_3_relies_on_rel_orgs_for_vote_info_weighted >= dalp_c2_3_relies_on_rel_orgs_for_vote_info_weighted_median)*1
    )
```

# Save data

```{r}
  saveRDS(dalp_clean, "../cleaned-data/x-10-dalp.rds")
```
